home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / prtfonts / part01 / PrintFonts.c next >
C/C++ Source or Header  |  1990-07-08  |  10KB  |  312 lines

  1. /*PrintFonts V1.11 (c)1989,1990 by Dave Schreiber.  All rights reserved*/
  2. /*Must be freely distributed, except for shipping, copying, media, and*/
  3. /*related costs.*/
  4. /*V1.00 finished August 30, 1989  */
  5. /*V1.10 finished January 21, 1990 */
  6. /*V1.11 finished May 14, 1990     */
  7.  
  8. /*    New features:  can print in more than one column
  9.                      can determine the maximum number of columns to use
  10.                      can print an alternate description of a font
  11.                         (good for use with unreadable fonts like
  12.                         Cairo or Music).
  13.                      
  14.                      Aborts if it can't print out a page of fonts (V1.11)
  15. */
  16.  
  17. /*Compiled using Lattice C V5.04*/
  18.  
  19. #include "setup.h"
  20.  
  21.  
  22. int InterpretArgs();
  23. WORD PrintPage();
  24. DumpWindowToPrinter();
  25.  
  26. main(argc,argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.    struct AvailFontsHeader *afh;
  31.    ULONG afhsize; /*Size of AvailFontsHeader buffer*/
  32.    ULONG numfonts,curfont;
  33.    BYTE max,readable,both;
  34.    WORD columns;
  35.    WORD maxlen;
  36.    struct AvailFonts *af;
  37.    
  38.    if(argc==2 && argv[1][0]=='?')   /*User wants help*/
  39.    {
  40.       puts("PrintFonts V1.11");
  41.       puts("(c)1989, 1990 by Dave Schreiber.  All rights reserved.");
  42.       puts("Format for use:");
  43.       puts("  1>  PrintFonts [options]");
  44.       puts("Where options are any of the following:");
  45.       puts("  -r:  Print each font's name in a readable format");
  46.       puts(" -c#:  Use # number of columns");
  47.       puts("  -m:  Determine the max number of columns that can be");
  48.       puts("       used and do not print.");
  49.       puts("  -b:  Determines the maximum number of columns and");
  50.       puts("       prints using that number of columns");
  51.       exit(0);
  52.    }
  53.    
  54.       /*Parse the command line arguments and set the appropriate flags*/
  55.    if(!InterpretArgs(argc,argv,&max,&readable,&columns,&both))
  56.       exit(1000);
  57.  
  58.    if(columns<1)
  59.    {
  60.       puts("You must have at least one column!");
  61.       exit(2000);
  62.    }
  63.    
  64.    startup();  /*Open Libraries, etc.*/
  65.    afhsize=AvailFonts(NULL,NULL,AFF_MEMORY|AFF_DISK);
  66.    
  67.    afh=(struct AvailFontsHeader *)AllocMem(afhsize,MEMF_CLEAR); 
  68.       /*Gets a buffer large enough to hold all the font information*/
  69.       /*(AvailFonts(0,0,[type]) returns the amount of memory needed*/
  70.       /*to hold the information for all the fonts of type [type])  */
  71.  
  72.    if(afh==NULL)  /*Could't allocate the memory*/
  73.    {
  74.       closeup();
  75.       ExitProg("Couldn't allocate enough memory!");
  76.    }
  77.    
  78.    AvailFonts(afh,afhsize,0xff); /*Get the info for fonts*/
  79.  
  80.    TopazAttr.ta_Name="topaz.font"; /*Topaz is used if the user wants*/
  81.    TopazAttr.ta_YSize=8;           /*'readable' font names*/
  82.    TopazAttr.ta_Style=0;
  83.    TopazAttr.ta_Flags=FPF_ROMFONT|FPF_DISKFONT;
  84.    
  85.    if((Topaz=(struct TextFont *)OpenFont(&TopazAttr))==NULL)
  86.    {
  87.       FreeMem(afh,afhsize);
  88.       closeup();
  89.       ExitProg("Can't open Topaz 8 point font!!!!!!!!!!!!");
  90.    }
  91.     
  92.    numfonts=afh->afh_NumEntries;  /*Get the number of fonts*/
  93.    curfont=1;                     /*First font*/
  94.    af=(struct AvailFonts *)&afh[curfont];
  95.    while(curfont <= numfonts)
  96.       maxlen=PrintPage(&curfont,numfonts,af,columns,max,readable);
  97.          
  98.    maxlen=MAX_X/maxlen;
  99.    if(maxlen < 1)
  100.       maxlen=1;
  101.       
  102.    if(max)  /*If just finding the max length*/
  103.    {
  104.       printf("These fonts can be printed in %d column",maxlen);
  105.       if(maxlen==1)
  106.          printf(".\n");
  107.       else
  108.          printf("s.\n");
  109.    }
  110.    
  111.    if(both) /*If -b switch, do it again, but print this time*/
  112.    {
  113.       curfont=1;
  114.       af=(struct AvailFonts *)&afh[curfont];
  115.       while(curfont <= numfonts)
  116.          PrintPage(&curfont,numfonts,af,maxlen,FALSE,readable);
  117.    }
  118.  
  119.    FreeMem(afh,afhsize);    /*Free up the allocated memory*/
  120.  
  121.    CloseFont(Topaz);
  122.    closeup();
  123. }
  124.  
  125. int InterpretArgs(argc,argv,max,readable,cols,both)
  126. int argc;
  127. char *argv[];
  128. BYTE *max,*readable,*both;
  129. WORD *cols;
  130. {
  131.    BYTE c;
  132.    int columns;
  133.    
  134.    *cols=1;    /*Load in some defaults*/
  135.    *max=FALSE;
  136.    *readable=FALSE;
  137.    *both=FALSE;
  138.    
  139.    for (c=1;c<argc;c++)    /*For each argument*/
  140.       switch(argv[c][1])   /*Letter after the slash*/
  141.       {
  142.          case 'b':      /*For -b switch, set both*/
  143.          case 'B':
  144.             *both=TRUE; /*AND max*/
  145.          case 'm':   /*Max columns mode*/
  146.          case 'M':   /*Just in case the user typed -T instead of -t*/
  147.             *max=TRUE;
  148.             break;
  149.          case 'r':   /*Readable*/
  150.          case 'R':
  151.             *readable=TRUE;
  152.             break;
  153.          case 'c':   /*Number of columns*/
  154.          case 'C':
  155.             if(argv[c][2]==NULL)
  156.                break;
  157.             stcd_i(&argv[c][2],&columns); /*Convert text to #*/
  158.             *cols=columns;
  159.             break;
  160.          default:
  161.             puts("I don't understand option:");
  162.             puts(argv[c]);
  163.             return(FALSE);
  164.       }
  165.    return(TRUE);
  166. }
  167.  
  168.    /*Setup and print a page of fonts*/
  169. WORD PrintPage(curfont,numfonts,af,cols,findmax,readable)
  170. ULONG *curfont;
  171. ULONG numfonts;
  172. struct AvailFonts *af;
  173. BYTE findmax,readable;
  174. WORD cols;
  175. {
  176.    
  177. #define FLAGS af->af_Attr.ta_Flags
  178. #define Rp Wdw->RPort
  179.  
  180.    BYTE Full=FALSE;
  181.    USHORT TextY=STARTINGROW;        /*Current Y coordinate*/
  182.    USHORT LastY;                    /*Last Y coordinate*/
  183.    static WORD max=0;
  184.    WORD length;
  185.    WORD column,StartX;
  186.    WORD ColWidth;
  187.    
  188.    char Master[256],fontstr[256],SizeStr[6],*string;
  189.    struct TextFont *tf;
  190.    struct TextAttr TFont;
  191.  
  192.    ColWidth=MAX_X/cols; /*Get the # of pixels in each column*/
  193.    
  194.    string = (char *)&Master[1]; /*Initialize a pointer and*/
  195.    Master[0]='(';       /*a string*/
  196.    
  197.    SetAPen(Rp,0);
  198.    RectFill(Rp,0,0,MAX_X-1,MAX_Y-1);
  199.    SetAPen(Rp,1);
  200.    ClearScreen(Rp);     /*Clear the bitmap*/
  201.    af+=(*curfont-1);
  202.    
  203.    
  204.    for(column=StartX=0;column<cols && *curfont <= numfonts;
  205.          StartX=(++column*ColWidth)) /*Loop for each column*/
  206.    {
  207.       TextY=STARTINGROW;
  208.       Full=FALSE;
  209.       
  210.       while(!Full && (*curfont <= numfonts))
  211.       {      
  212.          /*If the font is:  not removed, goes from left to right*/
  213.          /*and is EITHER a disk or memory font, do nothing.*/
  214.          /*(This is partly from the RKM)*/
  215.          if((af->af_Attr.ta_Style==0) &&
  216.          !( ((FLAGS & FPF_REMOVED)||(FLAGS & FPF_REVPATH) ||
  217.                ((FLAGS & FPF_DISKFONT) && (af->af_Type & AFF_MEMORY))) ) )
  218.             if((af->af_Attr.ta_YSize) < (MAX_Y-(4+TextY))) /*If it'll fit...*/
  219.             {
  220.             
  221.                TFont.ta_Name=(char *)af->af_Attr.ta_Name;  /*Get the name*/
  222.                TFont.ta_YSize=af->af_Attr.ta_YSize; /*Get the size*/
  223.                TFont.ta_Style=af->af_Attr.ta_Style; /*Plain style*/
  224.                TFont.ta_Flags=FPF_ROMFONT|FPF_DISKFONT|
  225.                      FPF_PROPORTIONAL|FPF_DESIGNED;  /*Straight from RKM*/
  226.             
  227.                if((tf=(struct TextFont *)OpenDiskFont(&TFont))!=NULL)
  228.                {
  229.                      /*Position 'cursor' at baseline*/
  230.                   TextY+=(LastY=(tf->tf_Baseline));
  231.                   strcpy(fontstr,af->af_Attr.ta_Name);
  232.                   strcpy(string,fontstr);  /*Get font name*/
  233.                   string[strlen(string)-5]=NULL;
  234.                   strcat(string," ");      /*Tack on a space*/
  235.                   stcu_d(SizeStr,af->af_Attr.ta_YSize); /*Get the size in chars*/
  236.                   strcat(string,SizeStr);
  237.                   strcat(string," ");
  238.                   
  239.                   Move(Rp,StartX,TextY);               
  240.                   SetFont(Rp,tf);      /*Set the font*/
  241.                   Text(Rp,string,strlen(string));  /*Print the font info*/
  242.                
  243.                         /*Get the length of the text string in pixels*/
  244.                   length=TextLength(Rp,string,strlen(string));
  245.                   
  246.                   if(readable) /*If -r switch...*/
  247.                   {
  248.                      Master[strlen(Master)-1]=')';
  249.                      SetFont(Rp,Topaz);
  250.                      Text(Rp,Master,strlen(Master));
  251.                      length+=TextLength(Rp,Master,strlen(Master));
  252.                   }
  253.                   
  254.                   if(length > max)
  255.                      max=length; /*And store in max if the biggest*/
  256.    
  257.                      /*Add decender area to height*/
  258.                   TextY+=(LastY=(tf->tf_YSize-tf->tf_Baseline+2));
  259.                   CloseFont(tf);   /*Close the font*/
  260.                }
  261.                (*curfont)++;
  262.                af++;
  263.             }
  264.             else
  265.                Full = TRUE;
  266.          else     
  267.          {
  268.             (*curfont)++;
  269.             af++;
  270.          }  
  271.       }
  272.    }
  273.    
  274.    if(!findmax)   /*Print fonts if -m switch isn't set*/
  275.       if((DumpWindowToPrinter(Wdw,printerMsg,
  276.             (TextY+LastY>MAX_Y ? MAX_Y : TextY+LastY))) != 0)
  277.       {
  278.          puts("Printer error!  Aborting!");
  279.          *curfont=numfonts+1; /*An error occured, so abort*/
  280.       }
  281.          
  282.    /*Add a little for serifs, but not more than MAX_Y*/
  283.    return(max); /*And return the max text length*/
  284. }
  285.  
  286. DumpWindowToPrinter(Wdw,request,LastY)
  287. struct Window *Wdw;
  288. union printerIO *request;
  289. USHORT LastY;
  290. {
  291.    struct ViewPort *Vp;
  292.    char FormFeed[2];
  293.    FormFeed[0]=0x0c;       /*Form feed character*/
  294.    FormFeed[1]=NULL;       /*NULL terminator*/
  295.    Vp=&Wdw->WScreen->ViewPort;
  296.    /*Set up for dumping the rastport*/
  297.    request->iodrp.io_Command=PRD_DUMPRPORT;
  298.    request->iodrp.io_RastPort=Rp;
  299.    request->iodrp.io_ColorMap=Vp->ColorMap;
  300.    request->iodrp.io_Modes=Vp->Modes;
  301.    request->iodrp.io_SrcX=0;
  302.    request->iodrp.io_SrcY=0;
  303.    request->iodrp.io_SrcWidth=MAX_X;
  304.    request->iodrp.io_SrcHeight=MAX_Y;
  305.    request->iodrp.io_DestCols=0;
  306.    request->iodrp.io_DestRows=0;
  307.    request->iodrp.io_Special=SPECIAL_FULLCOLS|
  308.             SPECIAL_FULLROWS;
  309.    return(DoIO(request)); /*Do the actual dump*/
  310. }
  311.  
  312.